package com.ctrip.ec.api.admin;

import com.ctrip.ec.api.admin.common.Encryption;

import java.util.concurrent.*;

/**
 * Created by jiang on 2018/8/22.
 */
public class Sample implements Runnable {


    public Sample() {
    }

    public Sample(CountDownLatch countDownLatch, ConcurrentHashMap<Integer, Integer> maps, boolean isWrite) {

        this.countDownLatch = countDownLatch;
        this.maps = maps;
        this.isWrite = isWrite;

    }

    private CountDownLatch countDownLatch;
    private ConcurrentHashMap<Integer, Integer> maps;
    private boolean isWrite;

    private  volatile  int abc ;

    private int keyCount = 500 * 10000;

    private int[] vals = new int[keyCount];

    @Override
    public void run() {

        if (isWrite) {
            Long now = System.currentTimeMillis();

            for (int i = 0; i < keyCount; i++) {
                maps.put(i, i);
            }

            System.out.println("write" + (System.currentTimeMillis() - now));
            countDownLatch.countDown();
        } else {
            Long now = System.currentTimeMillis();

            for (int i = 0; i < keyCount; i++) {
                while (maps.containsKey(i) == false) {
                    try {
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                vals[i] = maps.get(i);
            }

            System.out.println("read" + (System.currentTimeMillis() - now));


            countDownLatch.countDown();
        }

    }


    public static void main(String[] args) throws Exception {


//        Thread th = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Thread.currentThread().setDaemon(true);
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                String name = Thread.currentThread().getName();
//                String tid = Thread.currentThread().getId() + "";
//                boolean  isDaemon =  Thread.currentThread().isDaemon();
//                System.out.println(name + "|" + tid + "|" + isDaemon);
//
//            }
//        });
//
//        th.start();


        ExecutorService pool =   Executors.newFixedThreadPool(3, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread th = new Thread(r);
                th.setName("hello");
                th.setDaemon(true);
                return th;
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                String tid = Thread.currentThread().getId() + "";
                boolean  isDaemon =  Thread.currentThread().isDaemon();
                System.out.println(name + "|" + tid + "|" + isDaemon);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        pool.execute(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                String tid = Thread.currentThread().getId() + "";
                boolean  isDaemon =  Thread.currentThread().isDaemon();
                System.out.println(name + "|" + tid + "|" + isDaemon);

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        System.in.read();



//        String pwd = Encryption.md5("admin");
//        System.out.println(pwd);
    }


}
